package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.InvBomEntity;
import com.yvtian.inv.dao.InvBomMapper;
import com.yvtian.inv.pojo.InvBomShopEntity;
import com.yvtian.inv.service.InvBomService;
import com.yvtian.inv.service.InvBomShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * (inv_bom)表服务实现类
 *
 * @author long
 * @since 2024-05-09 14:28:14
 */
@Service("invBomService")
public class InvBomServiceImpl extends ServiceImpl<InvBomMapper, InvBomEntity> implements InvBomService {

    @Autowired
    private InvBomShopService invBomShopService;

    @Override
    public IPage<InvBomEntity> findByPage(Page<InvBomEntity> page, InvBomEntity invBomEntity) {
        LambdaQueryWrapper<InvBomEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvBomEntity::getIsDel, 0);
        if (StringHelper.isNotEmpty(invBomEntity.getCode())) {
            queryWrapper.like(InvBomEntity::getCode, invBomEntity.getCode());
        }
        if (StringHelper.isNotEmpty(invBomEntity.getName())) {
            queryWrapper.like(InvBomEntity::getName, invBomEntity.getName());
        }
        if (StringHelper.isNotEmpty(invBomEntity.getShopCode())) {
            queryWrapper.like(InvBomEntity::getShopCode, invBomEntity.getShopCode());
        }
        if (StringHelper.isNotEmpty(invBomEntity.getShopName())) {
            queryWrapper.like(InvBomEntity::getShopName, invBomEntity.getShopName());
        }
        if (invBomEntity.getStatus() != null) {
            queryWrapper.eq(InvBomEntity::getStatus, invBomEntity.getStatus());
        }
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvBomEntity invBomEntity) {
        ArrayList<InvBomShopEntity> invBomShopList = invBomEntity.getInvBomShopList();
        if (invBomShopList != null && !invBomShopList.isEmpty()) {
            LambdaUpdateWrapper<InvBomShopEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(InvBomShopEntity::getBomCode, invBomEntity.getCode());
            invBomShopService.remove(updateWrapper);
            invBomShopService.saveBatch(invBomShopList);
        }
        this.saveOrUpdate(invBomEntity);
        return true;
    }
}

